﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Diagnostics;

namespace MvcApplication1.Models
{
    public abstract class Sorter<T>
    {
        
        public abstract void Sort(ref T dv, string sortorder);
        
    }

    public class DataViewSorter : Sorter<DataView>
    {
        public override void Sort( ref DataView dv, string sortorder)
        {

            switch (sortorder)
            {
                case "itemSN":
                    dv.Sort = "itemSN";
                    break;
                case "itemSNDESC":
                    dv.Sort = "itemSN DESC";
                    break;
                case "addtime":
                    dv.Sort = "addTime";
                    break;
                case "addtimeDESC":
                    dv.Sort = "addTime DESC";
                    break;
            }
        }
    }
    public class IQueryableBrandsSorter : Sorter<IQueryable<Brand>>
    {
       
        //note that the dbset parameter must be passed by ref, if by value then the order change will lost when leaving the function
        public override void Sort( ref IQueryable<Brand> dbset, string sortorder)
        {
            switch (sortorder)
            {
                case "brandname":
                    dbset=dbset.OrderBy(b=>b.BrandName);
                    break;
                case "brandnameDESC":
                    dbset=dbset.OrderByDescending(b => b.BrandName);
                    break;
                case "registertime":
                    dbset=dbset.OrderBy(b => b.RegisterTime);
                    break;
                case "registertimeDESC":
                    dbset = dbset.OrderByDescending(b => b.RegisterTime);
                    break;
                case "itemcount":
                    dbset = dbset.OrderBy(b => b.itemCount);
                    break;
                case "itemcountDESC":
                    dbset = dbset.OrderByDescending(b => b.itemCount);
                    break;
            }
        }
    }
}